package com.siwei.mes.controller.common;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.system.Dict;
import com.siwei.mes.service.system.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 字典表相关
 *
 * @author linzi
 * @since 2023-11-25 22:22:20
 */
@Api(value = "字典表")
@RestController
@RequestMapping("dict")
public class DictController {
    @Resource
    private DictService dictService;

    @ApiOperation(value = "新增字典表")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) Dict entity) {
        return Json.succ().data(ApiResult.addColumn("id", dictService.create(entity)));
    }

    @ApiOperation(value = "修改字典表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) Dict entity) {
        dictService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除字典表")
    @PostMapping("/delete")
    public Json delete(@RequestBody Dict entity) {
        dictService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取字典表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Dict, Dict> query) {
        return Json.succ().data(dictService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取字典表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(dictService.loadById(id));
    }

    @ApiOperation(value = "获取所有字典表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody Dict entity) {
        return Json.succ().data(ApiResult.addList(dictService.getAll(entity)));
    }

}
